package com.firsteapps.mobile.android.puzzle;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.firsteapps.mobile.android.puzzle.util.Consts;
import com.firsteapps.mobile.android.puzzle.util.Utilities;

public class BaseActivity extends Activity {

	private PicturePuzzle mPicturePuzzle;
	private AudioManager audioManager;
	private final String TAG = "BaseActivity";

	public String getUserName() {
		return mPicturePuzzle.getUserName();
	}

	protected void setUserName(String selectedUserName) {
		mPicturePuzzle.setUserName(selectedUserName);
	}
	
	protected int getCreditBalance() {
		return mPicturePuzzle.getCredits();
	}

	public void setCreditBalance(int credits) {
		mPicturePuzzle.setCredits(credits);
	}

	protected String getWorkingDirectoryBase() {
		return getUserName() == null ? null : Consts.USERS_FOLDER + "/" + getUserName();
	}

	protected boolean[] getSettings() {
		return new boolean[] { mPicturePuzzle.isMusicEnabled(), mPicturePuzzle.isSoundEnabled(), mPicturePuzzle.isVibrationEnabled() };
	}

	protected void setSettings(boolean mSetting, boolean sSetting, boolean vSetting) {
		mPicturePuzzle.setMusicEnabled(mSetting);
		mPicturePuzzle.setSoundEnabled(sSetting);
		mPicturePuzzle.setVibrationEnabled(vSetting);
	}

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		Log.d(TAG, "onCreate");

		mPicturePuzzle = (PicturePuzzle) getApplication();
		mPicturePuzzle.initialisePlayer();
		if (mPicturePuzzle.isMusicEnabled() && !mPicturePuzzle.getPausePressed()) {
			mPicturePuzzle.resumePlayer();
		}

		audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		Log.d(TAG, "onPostCreate");

		final ImageView exitButton = (ImageView) findViewById(R.id.exit);
		if (exitButton != null) {
			exitButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					exitClicked(exitButton);
				}
			});
		}

		ImageView logo = (ImageView) findViewById(R.id.firsteapps_logo);
		if (logo != null) {
			logo.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.firsteapps.com")));
				}
			});
		}

		ImageView back = (ImageView) findViewById(R.id.back);
		if (back != null) {
			back.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Utilities.startActivityWithAnimation(BaseActivity.this, Home.class);
				}
			});
		}

		ImageView backToPostLogin = (ImageView) findViewById(R.id.back_to_postlogin);
		if (backToPostLogin != null) {
			backToPostLogin.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Utilities.startActivityWithAnimation(BaseActivity.this, PostLogin.class);
				}
			});
		}

		ImageView newsBar = (ImageView) findViewById(R.id.news_bar);
		if (newsBar != null) {
			newsBar.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Utilities.startActivityWithAnimation(BaseActivity.this, News.class);
				}
			});
		}

		ImageView addCredits = (ImageView) findViewById(R.id.hpl_add_credits);
		if (addCredits != null) {
			addCredits.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Utilities.startActivityWithAnimation(BaseActivity.this, ShopHere.class);
				}
			});
		}

		ImageView facebook = (ImageView) findViewById(R.id.facebook);
		if (facebook != null) {
			facebook.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com")));
				}
			});
		}

		ImageView twitter = (ImageView) findViewById(R.id.twitter);
		if (twitter != null) {
			twitter.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/firsteapps")));
				}
			});
		}

		ImageView toggleMusic = (ImageView) findViewById(R.id.toggle_music);
		if (toggleMusic != null) {
			toggleMusic.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mPicturePuzzle.isMusicEnabled()) {
						if (mPicturePuzzle.getPausePressed()) {
							mPicturePuzzle.resumePlayer();
						} else {
							mPicturePuzzle.pausePlayer();
						}
						mPicturePuzzle.setPausePressed(!mPicturePuzzle.getPausePressed());
					}
				}
			});
		}

		int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		final SeekBar volumeController = (SeekBar) findViewById(R.id.volume_controller);
		if (volumeController != null) {
			volumeController.setMax(maxVolume);
			volumeController.setProgress(curVolume);
			volumeController.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
				@Override
				public void onStopTrackingTouch(SeekBar arg0) {
					volumeController.setVisibility(View.INVISIBLE);
				}

				@Override
				public void onStartTrackingTouch(SeekBar arg0) {
				}

				@Override
				public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
					audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
				}
			});
		}

		ImageView musicVolume = (ImageView) findViewById(R.id.music_volume);
		if (musicVolume != null) {
			musicVolume.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (volumeController != null) {
						volumeController.setVisibility(View.VISIBLE);
					}
				}
			});
		}
	}
	
	protected void setPausePlaying(boolean pausePlaying) {
		mPicturePuzzle.setPausePlaying(pausePlaying);
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");

		if (mPicturePuzzle.getPausePlaying()) {
			mPicturePuzzle.pausePlayer();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");

		if (mPicturePuzzle.isMusicEnabled() && !mPicturePuzzle.getPausePressed()) {
			mPicturePuzzle.resumePlayer();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");

	}

	@Override
	public void onBackPressed() {
		// super.onBackPressed();
		// mPicturePuzzle.setPausePlaying(false);
		// mPicturePuzzle.releasePlayer();
		return;
	}

	private void exitClicked(final View v) {
		AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
		builder.setCancelable(true);
		builder.setTitle("Quit Application");
		builder.setMessage("Are you sure you want to exit the application?");
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				mPicturePuzzle.setPausePlaying(true);
				((Activity) v.getContext()).finish();
			}
		});
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
}